from odoo import models, api
from datetime import datetime, timedelta


class SbMESEmployee(models.Model):
    _inherit = "hr.employee"

    def cron_all(self):
        index = 1
        records = self.env['hr.employee'].sudo().search([])
        for x in records:
            x.write({'barcode': 'WTT%04d' % index})
            index += 1

        for rec in records:
            rec.employee_upload_mes()

    @api.model_create_multi
    def create(self, vals_list):
        result = super().create(vals_list)
        mes_enabled = self.env['s.api.config_parameter'].search([('s_key', '=', 'mes.enabled')], limit=1).s_value
        if mes_enabled:
            for rec in result:
                rec.with_delay().employee_upload_mes()
        return result

    def write(self, vals):
        res = super().write(vals)
        mes_enabled = self.env['s.api.config_parameter'].search([('s_key', '=', 'mes.enabled')], limit=1).s_value
        if mes_enabled:
            self.with_delay().employee_upload_mes()
        return res

    def employee_upload_mes(self):
        """
        员工
        """
        employee_list = []
        for record in self:
            if not record.job_id.s_code:
                continue
            if not record.barcode:
                continue
            employee_list.append({
                "org_id": "0001",
                "em_code": record.barcode,  # 员工编码
                "em_name": record.name,  # 员工名称
                "em_ename": "",  # 员工英文名称
                "xb": 1,  # 性别 1：男 2：女
                "mar": 1,  # 婚姻状态 1：未婚 2：已婚
                "stat": 1,  # 状态 1：在职 2：离职
                "isnew": "Y",  # 新人
                "indate": record.create_date.date().strftime("%Y-%m-%d %H:%M:%S"),  # 入职日期
                "xl": 1,  # 学历 1：小学 2：初中 3：中专/高中 4：专科 5：本科 6：硕士 7：博士
                "is_gps": "N",  # 定位卡
                "dept_code": record.department_id.code or "",  # 部门
                "birthday": "",  # 生日
                "age": 1,  # 年龄
                "trdays": 10,  # 试用期
                "cfdate": "",  # 转正日期
                "haddress": "",  # 家庭地址
                "htel_no": "",  # 家庭电话
                "xaddress": "",  # 现住地址
                "mobile": "",  # 手机
                "email": "",  # 邮件
                "qq": "",  # qq
                "wechat": "",  # 微信
                "nation": "",  # 民族
                "carid": "",  # 身份证
                "jg": "",  # 籍贯
                "user_code": "",  # 关联用户
                "inter_code": "",  # 面试人
                "inter_time": "",  # 面试时间
                "inter_desc": "",  # 面试评语
                "jk": "",  # 健康状况
                "out_rqdate": "",  # 离职申请日期
                "out_date": "",  # 离职日期
                "out_desc": "",  # 离职原因
                "remark": "",  # 备注
                "bs_emp_post": [{
                    "ps_code": record.job_id.s_code,
                    "rm_bdate": record.create_date.date().strftime("%Y-%m-%d %H:%M:%S"),
                    "rm_edate": (record.create_date.date() + timedelta(days=365)).strftime("%Y-%m-%d %H:%M:%S"),
                    "gop_code": "",
                    "aup_qty": 0,
                    "total_qty": 0,
                    "remark": ""
                }]
            })
        data = {
            "docType": "BS_EMP",
            "updateType": "UPDATE",
            "data": employee_list
        }
        self.env["s.mes.upload"].upload_data(data)
